'''
Descripttion: 
Author: 李增辉
Date: 2024-02-29 09:28:46
LastEditors: 李增辉
LastEditTime: 2024-02-29 15:19:36
'''
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import json

class ScrapyOnePipeline:
    #开启爬虫的时候打开文件
    def open_spider(self,spider):
        self.fp=open("古诗文.txt","w",encoding="utf-8")
   #这个process_item会在spider文件每yield一次就会调用一次
    def process_item(self, item, spider):
        self.fp.write(json.dumps(dict(item),ensure_ascii=False)+"\n")   #反斜杠n表示写完一行就换行     ensure_ascii放置文本变为阿斯克吗值
        return item  #return为了考虑可能还要其他的pipe要使用，在配置文件中配置
    
    def close_spider(self,spider):
        self.fp.close()#爬虫结束再关闭文件
